// pages/awardPool/awardPool.js
import {
    getbaseUrl,
    requestUtil
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hei:0,
        yaoShow:false,
        userInfo:{},
        awardData:[],
        baseUrl:""
    },

    // 领取奖励
    receive(e){
        let item = e.currentTarget.dataset.item
        let openId  = this.data.userInfo.openid
        // class为1是币
        if(item.class==1){
            // 金币的金额
            let money = item.prizeName.slice(0,2)
            // 奖品的id
            let awardId = item.awardId
            this.deduction(openId,money,awardId)

        }else{
            // console.log("是优惠券")
            this.getdiscountCard(item)
        }
        
    },

        // 兑换优惠券
        getdiscountCard(item){
    
            let disId = item.disId
            let awardId = item.awardId
            let openId = this.data.userInfo.openid
            let disActive = 2
            if(openId){
                wx.showLoading({
                    title: '数据加载中',
                  })
                let res = requestUtil({
                    url:"/shop/userAddDiscount",
                    method:"get",
                    data:{
                        disId,
                        openId,
                        disActive
                    }
                })
                res.then(({data})=>{
                    if(data.status==200){
                        this.deleteUserAward(openId,awardId)
                        wx.hideLoading()
                    }else{
                        wx.showToast({
                            title: '兑换优惠券失败',
                            icon:"error"
                          })
                          wx.hideLoading()
                    }
                    
                })
            }else{
                wx.showToast({
                    title: '你还没登陆呢，亲？',
                    icon:"error"
                  })
            }
    
        },

      // 狗加钱
   deduction(openId,totalPrice,awardId){
    let res = requestUtil({
        url:"/shop/noMatchPassword",
        method:"get",
        data:{
          openId,
          money:totalPrice
        }
      })
      res.then(r=>{
          if(r.data.status==200){
                // console.log("领取成功")
                // console.log(awardId)
                this.deleteUserAward(openId,awardId)
          }else{
          wx.showToast({
            title: '领取失败',
          })
          }
      })
  },

//   删除用户的奖品
deleteUserAward(openId,awardId){
   let res =  requestUtil({
        url:"/shop/deleteUserAward",
        data:{
            openId,
            awardId
        },
        method:"get"
    })
    res.then(r=>{
       if(r.data.status){
          this.getAwardData()
       }else{
        wx.showToast({
            title: '删除失败',
          })
       }
    })
},

    // 获取屏幕高度
    getHei(){
        wx.getSystemInfo({
          success: (result) => {
             let a= result.windowHeight
             this.setData({
                 hei:a
             })
          },
        })
    },

    // 显示隐藏
    faileShow(e){
        let id =e.currentTarget.dataset.id
        let index = this.data.awardData.findIndex(item=>{
           return item.awardId == id
        })
        this.data.awardData[index].index=this.data.awardData[index].index==0?1:0
        this.setData({
            awardData:this.data.awardData
        })
    },

    // 获取奖品数据
    getAwardData(){
        wx.showLoading({
          title: '数据加载中',
        })
        let openId = this.data.userInfo.openid
        let res = requestUtil({
            url:"/shop/getUserAward",
            method:"get",
            data:{
                openId
            }
        })
        res.then(r=>{
            if(r.data.status==200){
              let a=  r.data.data.map(item=>{
                    item.price = item.prizeName.slice(0,2)
                    // console.log(item)
                    return item
                })
                this.setData({
                    awardData:a
                })
                wx.hideLoading()
            }else{
                this.setData({
                    awardData:[]
                })
                wx.hideLoading()
            }
        })

    },

        // 获取本地的用户信息
        getUserInfo() {
            wx.getStorage({
                key: "userInfo",
                success: (res) => {
                    // res.data
                    this.setData({
                        userInfo: JSON.parse(res.data)
                    })
                    this.getAwardData()
                },
                fail: () => {
                    this.setData({
                        userInfo: {}
                    })
                }
    
            })
        },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getHei()
        this.getUserInfo()
        this.setData({
            baseUrl:getbaseUrl()+"/image/"
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})